<?php
class EleveManager
{
  private $_db;

  public function __construct($db)
  {
    $this->setDb($db);
  }

  public function add(Eleve $eleve)
  {
    $q = $this->_db->prepare('INSERT INTO eleve SET nom = :nom, prenom = :prenom, datenaissance = :datenaissance, password = :password, idRefClasse = :idRefClasse');

    $q->bindValue(':nom', $eleve->nom());
    $q->bindValue(':prenom', $eleve->prenom());
    $q->bindValue(':datenaissance', $eleve->datenaissance());
    $q->bindValue(':password', $eleve->password());
    $q->bindValue(':idRefClasse', $eleve->idRefClasse(), PDO::PARAM_INT);

    $q->execute();
  }

  public function delete(Eleve $eleve)
  {
    $this->_db->exec('DELETE FROM eleve WHERE id = '.$eleve->id());
  }

  public function get($id)
  {
    $id = (int) $id;

    $q = $this->_db->query('SELECT * FROM eleve WHERE id = '.$id);
    $donnees = $q->fetch();
    return new Eleve($donnees);
  }

  public function getList()
  {
    $eleves = array();

    $q = $this->_db->query('SELECT * FROM eleve ORDER BY id');

    while ($donnees = $q->fetch())
    {
      $eleves[] = new Eleve($donnees);
    }

    return $eleves;
  }
  
  public function getByClasse($classe) //Renvoie tous les élèves d'une classe
  {
    $eleves = array();

    $q = $this->_db->query('SELECT * FROM eleve WHERE idRefClasse = '.$classe.' ORDER BY id');

    while ($donnees = $q->fetch())
    {
      $eleves[] = new Eleve($donnees);
    }

    return $eleves;
  }

  public function update(Eleve $eleve)
  {
    $q = $this->_db->prepare('UPDATE eleve SET nom = :nom, prenom = :prenom, datenaissance = :datenaissance, idRefClasse = :idRefClasse WHERE id = :id');

    $q->bindValue(':prenom', $eleve->prenom());
    $q->bindValue(':nom', $eleve->nom());
    $q->bindValue(':datenaissance', $eleve->datenaissance());
    $q->bindValue(':idRefClasse', $eleve->idRefClasse(), PDO::PARAM_INT);
    $q->bindValue(':id', $eleve->id(), PDO::PARAM_INT);

    $q->execute();
  }

  public function setDb(PDO $db)
  {
    $this->_db = $db;
  }
  
  public function connexion($code) //Etablie une session de connexion pour un utilisateur
  {
				$p = $this->_db->query('SELECT * FROM eleve WHERE password = "'.$code.'"');
				$password = $p->fetch();
				if(strtoupper($password['password']) == strtoupper($code)){
					$_SESSION["Eleve"] = $this->get($password['id']);
					return array("Status" => "Connexion reussite", "Eleve" => serialize($_SESSION["Eleve"]));	
				} else { return array("Status" => "code d'accès incorrect", "Eleve" => NULL); }	
  }
  
  public function deconnexion() //Detruit la session
  {
	$_SESSION = NULL;
	session_destroy();  
  }
  
  public function generate($nombre, $idrefclasse) //Genère un nombre de code d'accès donné pour une classe donné
  {
	for($i = 0; $i < $nombre; $i++)
	{
		$q = $this->_db->prepare('INSERT INTO eleve SET password = :password, idRefClasse = :idRefClasse');
		$q->bindValue(':password', uniqid());
		$q->bindValue(':idRefClasse', $idrefclasse);
	
		$q->execute();
	}
  }
}
?>